package com.backend.models;

import com.backend.struts.GlobalDefine.PRODUCT_STATUS;
import io.ebean.ExpressionList;
import io.ebean.Finder;
import io.ebean.Model;
import lombok.Data;

import javax.persistence.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Entity
@Table(name = "product")
@Data
public class Product extends Model {
    @Id
    public Integer id;

    //0 已删除  1 正在出售 2 已售出 3 售后中
    @Enumerated
    public PRODUCT_STATUS status = PRODUCT_STATUS.insale;

    public Integer userId;

    @ManyToOne
    @JoinColumn(name = "genre_id")
    public ProductGenre genre;

    //这个外键的表暂时未建
    public Integer messageId;

    public Date createTime;
    public Double price;
    public Double buyPrice;//购买价格
    public Double expressFee;//快递费用
    public String description;
    public String videoPath;
    public Integer integrity;
    public Boolean needSelf = false;//自取
    public Boolean needMeet = false;//见面交易
    public Boolean needExpress = false;//邮寄
    public String showImg; // 展示图
    public String title;
    public Double lat = 0.0D;
    public Double lng = 0.0D;
    public Integer popularity = 0;

    public List<ImageStorge> getImgs(){
        List<ImageStorge> imageStorges = ImageStorge.search().eq("category", ImageStorge.IMAGE_TYPE.PRODUCT).eq("deleted",false).eq("boomId",id)
                .orderBy("orderNum asc").setFirstRow(0).setMaxRows(2).findList();
        return imageStorges;
    }

    public static Finder<Integer, Product> find = new Finder<>(Product.class);

    public static ExpressionList<Product> search() {
        return Product.find.query().where();
    }
}
